home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2036 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question about system call
  5. Date: 18 Jan 1996 17:09:27 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan18120927@g7240065.bridge.bst.bls.com>
  8. References: <4dekv8$9ja@spider.hik.se>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: Stefan Johansson's message of 15 Jan 1996 22:37:28 GMT
  11.  
  12. In article <4dekv8$9ja@spider.hik.se> Stefan Johansson <mia95jos@mc.hik.se> writes:
  13.  
  14. : Can someone help me with this little problem ?
  15.  
  16. : If I use a system call to write to standard output, how do I makre this poossible with an integer
  17. : value ?
  18.  
  19. : An example:
  20.  
  21. :   write(0, (the integer value), sizeof(int));
  22.  
  23. There can be two interpretations to this problem:
  24.  1) You want the integer value directly written to standard output.
  25.  2) You want the ascii representation of the integer value to be written
  26.     to standard output.
  27.  
  28. 1) Try:
  29.  
  30.   write(0, &int_value, sizeof(int));
  31.  
  32. 2) Try:
  33.  
  34.   char buf[10];
  35.   sprintf(buf, "%d", int_value);
  36.  
  37.   write(0, buf, strlen(buf)-1);
  38.  
  39. A couple of points, this is not portable, why not use the stdio library,
  40. this would then become
  41.  
  42. 1)
  43.   printf("%*s", sizeof(int), (char*)&int_value);
  44.  
  45. 2)
  46.   printf("%d", int_value);
  47. or equivalently
  48.   fprintf(stdout, "%d", int_value);
  49.  
  50.  
  51. Secondly, on UNIX machines (which I am assuming you are using), file
  52. descriptor '0' is stdin and '1' is stdout, so the write should be:
  53.  
  54.   write(1, buf, strlen(buf));
  55.  
  56. NB: The write is system dependent, so I am assuming:
  57.   int write(int fildes, const void* buf, size_t nbytes);
  58.  
  59. Regards
  60.  
  61.    -A.
  62. -- 
  63. | A.Champion                |
  64.